package jdbc1;

import org.junit.Test;

import java.sql.*;
import java.util.Scanner;

/**
 * @Auther:lele
 * @Date:2025/3/11
 */
public class jdbcStudy {
    public static void main(String[] args) throws Exception {
        jdbcStudy jdbcStudy = new jdbcStudy();
//        jdbc1.jdbcStudy.show();
////        jdbc1.jdbcStudy.insertData();
//        jdbc1.jdbcStudy.deleteData();
//        jdbc1.jdbcStudy.show();
        Scanner scanner = new Scanner(System.in);
        String admin = scanner.next();
        String password = scanner.next();
        System.out.println("jdbc1.jdbcStudy.a(admin,password) = " + jdbcStudy.a(admin, password));
    }
    @Test
//    检测用户和密码
    public boolean a(String admin, String password) throws Exception {
        Connection conn = conn();
        Statement statement = conn.createStatement();
        String sql = "select * from user where admin='"+admin+"'and password='"+password+"'";
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet.next();
    }


    public Connection conn() throws Exception {
//        1.注册操作的驱动  反射
        Class.forName("com.mysql.cj.jdbc.Driver");
//        2使用api连接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ma?useSSL=false&serverTimezone=UTC", "root", "123456");
        return conn;
    }

    @Test
    public void show() {
        try {
            Connection conn = this.conn();

//        3定义查询语句
            String sql = "select *from course";
//        4.获取查询对象
            Statement statement = conn.createStatement();
            System.out.println("statement=" + statement);
//        5使用查询对象执行sql
            ResultSet resultSet = statement.executeQuery(sql);
            System.out.println("resultSet = " + resultSet);
//        6.解析数据
            while (resultSet.next()) {
                int course_id = resultSet.getInt("course_id");
                String course_name = resultSet.getString("course_name");
                System.out.println(course_id + "|" + course_name);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //增
    public void insertData() {
        try {
            Connection conn = conn();
//        定义查询语句
            String sql = "insert into course values(null,'321')";
//        获取添加对象
            Statement statement = conn.createStatement();
            int i = statement.executeUpdate(sql);
            System.out.println(i);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
//    删除
    public void deleteData() throws Exception {
//        注册操作数据的驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
//        使用api连接数据库
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ma?useSSL=false&serverTimezone=UTC", "root", "123456");
        String sql = "delete from course where course_name='数据库'";
        // String sql = "delete from student where stu_id=7";
//        获取添加对象
        Statement statement = conn.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("i = " + i);
    }

    @Test
//    修改
    public void upData() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ma?useSSL=false&serverTimezone=UTC", "root", "123456");
        String sql = "update student set stu_sex='男'where stu_id=10";
        Statement statement = conn.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println("i = " + i);
    }
}

